﻿var macro;
var jsLF = "\n";
var i, retcode, errtext;
var MyDelay;
var MyPosts;

var Aldeias;
var Blindados;

var espioes;
var cavalariaLeve;
var command;
var aldeiaAtacante = "43215";
var pontuacaoMaxima = 140;

var farm = "";


i = 0;

espioes = 1;
cavalariaLeve = 15;
var log =  "";
MyDelay = 0;

var inicio = 0;
var quantAtaques = 200;
//command = commandPega();


Blindados = new Array();
Aldeias = new Array();

//Aldeias.push("45890");

//maiores
//Aldeias.push("19761");
//Aldeias.push("19476");
//Aldeias.push("18665");
//Aldeias.push("18737");
//Aldeias.push("19118");
//Aldeias.push("17389");
//Aldeias.push("19515");
//Aldeias.push("17737");
//Aldeias.push("19962");
//Aldeias.push("20310");
//Aldeias.push("17331");




//
//a1 = "32667";
//if(!conteins(a1)){
//   Aldeias.push(a1);
//}


//Blindados.push("56119");




//dados da aldeia

//coordenadas
macro = "CODE:";
macro += "TAG POS=1 TYPE=TABLE ATTR=ID:header_info EXTRACT=HTM";
iimPlay(macro);

var dado = iimGetLastExtract();
    
var coordenada = dado.substring(dado.indexOf(">(")+2, dado.indexOf(") K"));
    
//mundo    
macro = "CODE:";
macro += "ADD !EXTRACT {{!URLCURRENT}}";
iimPlay(macro);
var text = iimGetLastExtract();
mundo = text.substr(7, 4);

//alert(mundo);

aldeiaAtacante = text.substring(text.indexOf("village=")+8, text.indexOf("&")-0);
//alert(aldeiaAtacante);
//captura do site

macro = "CODE:";
//macro += "URL GOTO=http://br.twstats.com/"+mundo+"/index.php?page=village_locator&stage=4&source=village&village_coords="+coordenada+"&searchstring=&tribe_id=0&filter=abandoned" + jsLF;
macro += "URL GOTO=http://br.twstats.com/"+mundo+"/index.php?page=village_locator&stage=4&source=village&village_coords="+coordenada+ jsLF;
macro += "TAG POS=1 TYPE=TABLE ATTR=CLASS:vis EXTRACT=HTM";
//macro += "TAG POS=1 TYPE=DIV ATTR=ID:main EXTRACT=HTM";
iimPlay(macro);

var statics = iimGetLastExtract();

var j = 0;
while(true){
    //alert(statics);

    var num1 = statics.indexOf("<tr class");
    
    if(num1==-1){
        break;
    }
    
    var num2 = statics.indexOf("</tr>")+5;
    
    var line = statics.substring(num1, num2);
    //    alert(line);

    statics = statics.substr(num2, statics.length);
    
    
    //    var ald = line.substr(line.indexOf(">Aldeia de bárbaros (") - 10, 20);
    //    ald = ald.substring(ald.indexOf(";id=")+4, ald.indexOf(">")-1);
    //    
    //var ald = line.substr(line.indexOf(">Aldeia de bárbaros (") - 10, 20);
    var ald = line.substring(line.indexOf("<a href=")+0, line.indexOf("border")-2);
    ald = ald.substring(ald.indexOf("target=")+7, ald.length);

    var pt = line.substring(line.length-19, line.length);
    pt = pt.substring(pt.indexOf("<td>")+4, pt.length)
    pt = pt.substring(0, pt.indexOf("</td>"));


    var barb = line.indexOf("<td><a href=\"index.php?page=player&amp;id=0\"></a></td>")


    //<tr class="r1"><td><a href="http://br38.tribalwars.com.br/game.php?village=18190&amp;screen=place&amp;mode=command&amp;target=19227" border="0" target="_blank"><img src="/img/attack.png" target="_blank"></a></td><td>1,41</td><td><a href="index.php?page=village&amp;id=18190">000000 (635|529)</a></td><td><a href="index.php?page=player&amp;id=1387642">edilsonfc</a></td><td><a href="index.php?page=tribe&amp;id=1022">OR</a></td><td><a href="index.php?page=village&amp;id=19227">Aldeia-bonus (636|528)</a></td><td><a href="index.php?page=player&amp;id=0"></a></td><td><a href="index.php?page=tribe&amp;id="></a></td><td>63</td></tr>



    //    alert(barb);
    



    //    alert(pt);


    if(ald!=""){
        if(j >= inicio){
            
            if(barb > 0){
                Aldeias.push(ald);
                log = (j++) + "Encontrado "+ald + jsLF + log;
                iimDisplay(log);
//                alert("barb: " +pt );
            }else if(pt< pontuacaoMaxima){
                Aldeias.push(ald);
                log = (j++) + "Encontrado "+ald + jsLF + log;
                iimDisplay(log);
                farm+= "Farms38.push(\""+ald+"\");\n"
//                alert(pt);
            }
            
        }else{
            log = (j++) + "Recusado "+ald + jsLF + log;
        }
    }
    
    if(j>=quantAtaques){
        break;
    }
}


alert(farm);

var ataques = Aldeias.length;
if(quantAtaques < Aldeias.length)
    ataques = quantAtaques;


for (i ; i < ataques; i++) {
    
    if(isBlindado(Aldeias[i])){
        continue;
    }
    
    log = "Executando "+i+ " de " + (ataques -1) +jsLF + log;
    
    iimDisplay(log);
    //    43215
    iimSet("POSTID", Aldeias[i]);
    
    macro = "CODE:";
    macro += "URL GOTO=http://"+mundo+".tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&target={{postid}}" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    iimPlay(macro);

    var text = iimGetLastExtract();
     
    var numCavalariaLeve = text.substr(text.indexOf("('#unit_input_light')")+23,10);
    numCavalariaLeve = numCavalariaLeve.substr(0,numCavalariaLeve.indexOf(")"));
    numCavalariaLeve = parseInt(numCavalariaLeve);
    //  alert(numCavalariaLeve);

    if(numCavalariaLeve<cavalariaLeve){
        //alert("não ha cavalos suficientes. Necessário "+cavalariaLeve);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;
    }

    var numSpy = text.substr(text.indexOf("('#unit_input_spy')")+20,10);
    numSpy = numSpy.substr(0,numSpy.indexOf(")"));
    numSpy = parseInt(numSpy);
    //    alert(numSpy);

    if(numSpy<espioes){
        //alert("não ha espioes. Necessário "+espioes);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;

    }


    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    
    iimPlay(macro);

    text = iimGetLastExtract();
    
    
    var error = text.indexOf(">O alvo só pode ser atacado ou atacar a partir de");
    
    if(error>0){
        log = log+" - er";
        continue;
    }
    
    command = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    
    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://"+mundo+".tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&action=command&h="+ command +" ATTR=ID:troop_confirm_go" + jsLF;
    macro += "WAIT SECONDS=" + MyDelay + jsLF;
    

    retcode = iimPlay(macro);
    if (retcode < 0) {              // an error has occured
        errtext = iimGetLastError();
        alert(errtext);
        i--;

    //commandPega();
        
    // break;
    }
}







function commandPega(){
    var macro = "CODE:";
    macro += "URL GOTO=http://"+mundo+".tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&target=32667" + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    //macro += "TAG POS=1 TYPE=BODY ATTR=CLASS:* EXTRACT=HTM";
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
	
    var retcode = iimPlay(macro);

    //retcode = iimPlay(macro);
    	
    var text = iimGetLastExtract();
    text = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    alert(text);
    
    return text;
}

function conteins(aldeia){
    var i;
    for(i = 0; i<Aldeias.length;i++){
        if(Aldeias[i]== aldeia)
            return true;
    }
    return false;
}

function isBlindado(aldeia){
    var i;
    for(i = 0; i<Blindados.length;i++){
        if(Blindados[i]== aldeia)
            return true;
    }
    return false;
}



